<template>
  <div class="container">
    <el-form :model="artilceItem" label-width="80px" :rules="rules"  ref="articleForm">
      <el-form-item label="活动名称" prop='title'>
        <el-input v-model="artilceItem.title" placeholder="请输入名称"></el-input>
      </el-form-item>
      <!-- 富文本编辑器校验规则prop必须是data数据项和规则项一致 -->
      <el-form-item label="文章内容" prop="articleBody">
        <!-- 富文本的失去焦点事件，在事件处理函数中对改字段进行校验即可 -->
        <quill-editor v-model="artilceItem.articleBody" :options="editorOption"  @blur="checkContent" />
        <!-- editorOption 配置对象：配置富文本有什么功能 -->
      </el-form-item>
      <el-form-item label="视频地址" prop='videoUrl'>
        <el-input v-model="artilceItem.videoURL" placeholder="请输入视频地址"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="articleclose">取消</el-button>
        <el-button type='primary' @click="submitForm" >确认</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { add, update } from './../../api/hmmm/articles'
export default {
  name: 'page-articleadd',
  data () {
    return {
      artilceItem: {
        title: '',
        // 因为form表单已经绑定了artilceItem，因此只写articleBody就行
        articleBody: null,
        videoURL: ''
      },
      // 富文本编辑器配置对象
      editorOption: {
        placeholder: '请输入文章内容',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'],
            [{ list: 'ordered' }, { list: 'bullet' }],
            ['blockquote', 'code-block'],
            ['image']
          ]
        }
      },
      // 校验规则
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        // 富文本编辑器校验
        articleBody: [{ required: true, message: '请输入文章', trigger: 'blur' }]
      }
    }
  },
  props: {
    articleEdit: {
      type: Object
    },
    cleardata: {
      type: Object
    }
  },
  components: {
    quillEditor
  },
  created () {
    if (this.articleEdit.id) {
      // 根据父组件传过来的值是否有id有的话给内容赋值
      const { title, articleBody, videoURL } = this.articleEdit
      this.artilceItem = { title, articleBody, videoURL }
    } else {
      // 要是没有id就是新增文章传给空值，因为弹窗打开后值为空
      this.$refs.articleForm = {}
    }
  },
  methods: {
    // 关闭弹窗
    articleclose () {
      this.$emit('closeArticle')
    },
    // 提交时验证
    submitForm () {
      this.$refs.articleForm.validate(async (valid) => {
        if (valid) {
          try {
            if (this.articleEdit.id) {
              this.artilceItem.id = this.articleEdit.id
              await update(this.artilceItem)
              this.$refs.articleForm.resetFields()
              this.$emit('closeArticle')
              this.$message.success('修改成功')
            } else {
              await add(this.artilceItem)
              this.$refs.articleForm.resetFields()
              this.$emit('closeArticle', 1)
              this.$message.success('发布成功')
            }
          } catch (e) {
          }
        } else {
          console.log(0)
        }
      })
    },
    // 富文本编辑器验证
    checkContent () {
      // console.log(this.$refs.articleForm)
      this.$refs.articleForm.validateField('articleBody')
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .ql-editor {
  //让富文本编辑器高度250px
  height: 250px;
}
::v-deep .ql-toolbar.ql-snow {
  padding: 0 8px;
}

</style>
